//
//  MasonryViewController.m
//  视图位置任意调整
//
//  Created by Hello Cai on 2019/7/3.
//  Copyright © 2019 Hello Cai. All rights reserved.
//

#import "MasonryViewController.h"
#import "UIView+GHFrame.h"
#import "Masonry.h"

#define SCREEN_W [UIScreen mainScreen].bounds.size.width

@interface MasonryViewController ()
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UIView *v1;
@property(nonatomic, strong)UIView *v2;
@property(nonatomic, strong)UIView *v3;
@property(nonatomic, strong)UIView *v4;

@property(nonatomic, strong)MASConstraint *v1TopConstraint;
@property(nonatomic, strong)MASConstraint *v2TopConstraint;
@property(nonatomic, strong)MASConstraint *v3TopConstraint;
@property(nonatomic, strong)MASConstraint *v4TopConstraint;
@end

//https://www.jianshu.com/p/fad85f661e8e
@implementation MasonryViewController

/**
 把1,2,3,4 ==> 1,3,4 ==> 4,3,2,1
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.UIView *headerView = [[UIView alloc] init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *headerView = [[UIView alloc] init];
    self.headerView = headerView;
    headerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:headerView];
    
    //    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
    UIView *v1 = [[UIView alloc] init];
    v1.backgroundColor = [UIColor blueColor];
    [headerView addSubview:v1];
    self.v1 = v1;
    [v1 mas_makeConstraints:^(MASConstraintMaker *make) {
        _v1TopConstraint = make.top.mas_equalTo(v1.superview);
        make.width.mas_equalTo(SCREEN_W);
        make.height.mas_equalTo(60);
    }];
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab.text = @"视图1";
    [v1 addSubview:lab];
    
    
    
    //    UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v1.frame), SCREEN_W, 90)];
    UIView *v2 = [[UIView alloc] init];
    [headerView addSubview:v2];
    self.v2 = v2;
    v2.backgroundColor = [UIColor greenColor];
    [v2 mas_makeConstraints:^(MASConstraintMaker *make) {
        _v2TopConstraint = make.top.equalTo(v1.mas_bottom);
        make.width.mas_equalTo(SCREEN_W);
        make.height.mas_equalTo(90);
    }];
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab2.text = @"视图2";
    [v2 addSubview:lab2];
    
    
    //    UIView *v3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v2.frame), SCREEN_W, 120)];
    UIView *v3 = [[UIView alloc] init];
    [headerView addSubview:v3];
    self.v3 = v3;
    v3.backgroundColor = [UIColor yellowColor];
    [v3 mas_makeConstraints:^(MASConstraintMaker *make) {
        _v3TopConstraint = make.top.equalTo(v2.mas_bottom);
        make.width.mas_equalTo(SCREEN_W);
        make.height.mas_equalTo(120);
    }];
    UILabel *lab3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab3.text = @"视图3";
    [v3 addSubview:lab3];
    
    
    //    UIView *v4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(v3.frame), SCREEN_W, 160)];
    UIView *v4 = [[UIView alloc] init];
    [headerView addSubview:v4];
    self.v4 = v4;
    v4.backgroundColor = [UIColor purpleColor];
    [v4 mas_makeConstraints:^(MASConstraintMaker *make) {
        _v4TopConstraint = make.top.equalTo(v3.mas_bottom);
        make.width.mas_equalTo(SCREEN_W);
        make.height.mas_equalTo(160);
    }];
    UILabel *lab4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lab4.text = @"视图4";
    [v4 addSubview:lab4];
    
    headerView.width = SCREEN_W;
    headerView.height = v1.height+v2.height+v3.height+v4.height;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 500, 60, 40)];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}

-(void)btnClick:(UIButton *)button{
    
    button.selected = !button.selected;
    _v1.hidden = NO;
    _v2.hidden = NO;
    _v3.hidden = NO;
    _v4.hidden = NO;
    
    if (button.selected==YES) {
        //1，3，4
        _v2.hidden = YES;
        [_v1TopConstraint uninstall];//卸载，相当于删除某条约束
        //增加新约束
        [_v1 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v1TopConstraint = make.top.equalTo(_v1.superview);
        }];
        [_v3TopConstraint uninstall];
        [_v3 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v3TopConstraint = make.top.equalTo(_v1.mas_bottom);
        }];
        [_v4TopConstraint uninstall];
        [_v4 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v4TopConstraint = make.top.equalTo(_v3.mas_bottom);
        }];
    }else{
        //4，3，2，1
        [_v4TopConstraint uninstall];
        [_v4 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v4TopConstraint = make.top.equalTo(_v4.superview);
        }];
        [_v3TopConstraint uninstall];
        [_v3 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v3TopConstraint = make.top.equalTo(_v4.mas_bottom);
        }];
        [_v2TopConstraint uninstall];
        [_v2 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v2TopConstraint = make.top.equalTo(_v3.mas_bottom);
        }];
        [_v1TopConstraint uninstall];
        [_v1 mas_makeConstraints:^(MASConstraintMaker *make) {
            _v1TopConstraint = make.top.equalTo(_v2.mas_bottom);
        }];
    }
    [self.view layoutIfNeeded];
    _headerView.height = (_v1.hidden?0:_v1.height) + (_v2.hidden?0:_v2.height) + (_v3.hidden?0:_v3.height) + (_v4.hidden?0:_v4.height);
}

@end
